package com.xkstream.client.ui.adapter;

import android.content.Context;
import android.support.v7.widget.RecyclerView;
import android.view.View;
import android.view.ViewGroup;
import android.widget.TextView;

import com.xkstream.client.R;
import com.xkstream.client.base.BaseRecycleViewAdapter;
import com.xkstream.client.presenter.LiveRoomContract;
import com.xkstream.client.utils.DeviceUtils;

import butterknife.BindView;
import butterknife.ButterKnife;

/**
 * Created by xiaoyi on 2017-9-28.
 */

public class SelectMoneyAdapter extends BaseRecycleViewAdapter {
    private String[] mDatas;
    private int width;
    private int selected;

    public SelectMoneyAdapter(Context mContext, String[] mDatas) {
        super(mContext);
        this.mDatas = mDatas;
        width = (DeviceUtils.deviceWidth(mContext) - DeviceUtils.dp2px(mContext, 15) * 5) / 4;

    }

    @Override
    public RecyclerView.ViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
        View view = mInflater.inflate(R.layout.layout_select_money_item, parent, false);
        return new ItemHolder(view);
    }

    @Override
    public void onBindViewHolder(RecyclerView.ViewHolder holder, int position) {
        onBindItem((ItemHolder) holder, position);
    }

    private void onBindItem(ItemHolder holder, int position) {
        String money = mDatas[position];
        holder.tvMoney.setText(money);
        if (position == selected) {
            holder.tvMoney.setBackgroundResource(R.drawable.money_selected);
            holder.tvMoney.setTextColor(mContext.getResources().getColor(R.color.text_title));
        } else {
            holder.tvMoney.setBackgroundResource(R.drawable.money_unselected);
            holder.tvMoney.setTextColor(mContext.getResources().getColor(R.color.login_input_selected));
        }
        ViewGroup.LayoutParams params = holder.tvMoney.getLayoutParams();
        params.width = width;
        holder.tvMoney.setLayoutParams(params);
        holder.itemView.setOnClickListener((v) -> {
            selected = position;
            notifyDataSetChanged();
            if (mItemClickListener!=null){
                mItemClickListener.onItemClick(v,position);
            }
        });
    }

    @Override
    public int getItemCount() {
        return mDatas.length;
    }

    static class ItemHolder extends RecyclerView.ViewHolder {
        @BindView(R.id.tvMoney)
        TextView tvMoney;

        public ItemHolder(View itemView) {
            super(itemView);
            ButterKnife.bind(this, itemView);
        }
    }
}
